package gui.polish;

import gui.mpm.MPMFrame;
import gui.mpm.MPMPanel;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.border.TitledBorder;

import utilities.UpdatePelletsPerDayTimer;
import clients.RPCClient;
import java.awt.SystemColor;
import javax.swing.UIManager;
/**
 * 
 * @author adam
 * This Frame is the initializer of the polish Department GUI , it initializes the Polish main panel where it controls the jars table showing it's contents .
 */
public class PolishFrame extends JFrame  {
	
	//Variables
	public static RPCClient PolishClient = null;
    private PolishPanel myPanel;
	private static Dimension dim; // Will hold the location of the screen 
	
/**
 * PolishFrame - Constructor
 * @throws Exception
 */
	public PolishFrame() throws Exception
	{
		getContentPane().setBackground(UIManager.getColor("InternalFrame.activeTitleBackground"));
		setIconImage(Toolkit.getDefaultToolkit().getImage(MPMFrame.class.getResource("/graphics/Spearhead.png")));
    	setTitle("ח.נ.י.ת");
        initComponents();
    	this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
	}
/**
 * Initializes the frame with default settings .
 */
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        dim = Toolkit.getDefaultToolkit().getScreenSize();
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 559, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 481, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
/**
 * Initializes the polish panel , and calls the PolishPanel method "setVariables" with the parameters that was received from the DB.
 * @param myPanel	(PolishPanel)
 * @throws Exception
 */
	private void InitializeAllFiends(PolishPanel myPanel) throws Exception {
		// TODO Auto-generated method stub
		

		PolishClient= new RPCClient();
		ArrayList<Object> message = new ArrayList<>();
		ArrayList<Object> respond=null;
		message.add("initializePolishPane");
		try{
			respond = PolishClient.call(message);
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		if(respond.get(1).toString().equalsIgnoreCase("Valid")) 
    	System.out.println("Got me my array, returning to the original method.");
    	myPanel.setVariables(respond);
    	
	}



/**
 * Main !
 * calls the initComponents , then InitializeAllFiends
 * @param args
 * @throws Exception
 */


	public static void main(String args[]) throws Exception	 {
		 /* Set the Nimbus look and feel */
	        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
	        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
	         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
	         */
	        try {
	            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
	                if ("Nimbus".equals(info.getName())) {
	                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
	                    break;
	                }
	            }
	        } catch (ClassNotFoundException ex) {
	            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (InstantiationException ex) {
	            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (IllegalAccessException ex) {
	            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
	            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        }
	        //</editor-fold>
	        
	        /* Create client and connect to the Server*/

	        
	            try
	            {
	               PolishClient = new RPCClient();
					if (PolishClient == null)
						System.exit(0);

	               System.out.println("Client connection is up and ready.");

	               // get list of orders
	           }
	           catch  (Exception e) 
	           {
	             //e.printStackTrace();
	           }
	            
	            //Initializing the Polish Frame and Starting Panel 
	            
	             PolishFrame myFrame = new PolishFrame();
		   		 myFrame.myPanel = new PolishPanel(myFrame);
		   		 myFrame.initComponents();
				 myFrame.InitializeAllFiends(myFrame.myPanel);
	             myFrame.setContentPane(myFrame.myPanel);
	             myFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
	             myFrame.setLocation(dim.width/2-myFrame.getSize().width/2, dim.height/2-myFrame.getSize().height/2); 	//sets the location to be in the middle of the screen .
	            // UpdatePelletsPerDayTimer rem = new UpdatePelletsPerDayTimer(1);				// Calling the timer class to Update the pellets cycles once a day . sends 1 (day) as a parameter .
				 myFrame.setVisible(true);
				 
	 }
	
}
